#include <stdio.h>
#include "stm32f4xx.h"

#include "commands_4bits.h"


void delai(void)
{
	int i;
	for(i=0; i < 1000000; i++)
	;
}

// Voir document User Manual DM00031020.pdf
int main(void)
{
	int i;
  //printf("Test"); // BLOQUE !
	 // GPIOD Periph clock enable 
	 RCC->AHB1ENR   |= RCC_AHB1ENR_GPIODEN ;
	 RCC->AHB1LPENR |= RCC_AHB1LPENR_GPIODLPEN;
	 // Leds : Orange sur PD13, verte sur PD12, Rouge sur PD14, Bleu sur PD14
	 // On met tout le port D en GENERAL PURPOSE OUTPUT -> 0b01010101010101010101010101010101
	 GPIOD->MODER   = 0x55555555; 	// voir L654 stm32f4xx.h
	 GPIOD->OTYPER  = 0x00000000;  	// OUTPUT TYPE REGISTER Push Pull
	 GPIOD->OSPEEDR = 0xffffffff;		// 100 MHz
	 GPIOD->PUPDR   = 0x00000000;  	// No Pull up, No Pull Down 
	
	 // GPIOC Periph clock enable 
	 RCC->AHB1ENR   |= RCC_AHB1ENR_GPIOCEN ;
	 RCC->AHB1LPENR |= RCC_AHB1LPENR_GPIOCLPEN;
	 GPIOC->MODER   = 0x55555555; 	// voir L654 stm32f4xx.h
	 GPIOC->OTYPER  = 0x00000000;  	// OUTPUT TYPE REGISTER Push Pull
	 GPIOC->OSPEEDR = 0xffffffff;		// 100 MHz
	 GPIOC->PUPDR   = 0x00000000;  	// No Pull up, No Pull Down 
	
	 // GPIOB Periph clock enable 
	 RCC->AHB1ENR   |= RCC_AHB1ENR_GPIOBEN ;
	 RCC->AHB1LPENR |= RCC_AHB1LPENR_GPIOBLPEN;
	 GPIOB->MODER   = 0x55555555; 	// voir L654 stm32f4xx.h
	 GPIOB->OTYPER  = 0x00000000;  	// OUTPUT TYPE REGISTER Push Pull
	 GPIOB->OSPEEDR = 0xffffffff;		// 100 MHz
	 GPIOB->PUPDR   = 0x00000000;  	// No Pull up, No Pull Down 

	
	// !!!! il faut le laisser le temps a l'achiffeur du 20eme siecle de demarrer
	delai();
	delai();
	delai();
	//init_old();
	lcd_init();
	clear_display();
	write_char('S');
	write_char('T');
	write_char('M');	
  write_char('3');
	write_char('2');
	write_char('F');
	write_char('4');
	write_char('0');
	write_char('7');
	write_char('V');
	write_char('G');	
	write_char('T');
	write_char('6');
		write_char('-');
		line_feed();
			write_char('1');
				write_char('2');
					write_char('3');
						write_char('4');
	
	
	while(1)
	{	
	 // ON
	 //Tous les bits d'un coup : GPIOD->ODR = 0x0000f000;
		
	 GPIOD->BSRRL = 1<<12;  // Led VERTE sur PD12
   GPIOD->BSRRL = 1<<13;  // Led ORANGE sur PD13
	 GPIOD->BSRRL = 1<<14;  // Led ROUGE sur PD14
	 GPIOD->BSRRL = 1<<15;  // Led BLEUE sur PD15
	
   // Channel 1 sur IKALOGIC		
	 GPIOD->BSRRL = 1; 
   GPIOD->BSRRH = 2;		
	 delai();

	 // OFF
	 GPIOD->BSRRH = 1<<12;  // Led VERTE sur PD12
   GPIOD->BSRRH = 1<<13;  // Led ORANGE sur PD13
	 GPIOD->BSRRH = 1<<14;  // Led ROUGE sur PD14
	// GPIOD->BSRRH = 1<<15;  // Led BLEUE sur PD15
		
	 GPIOD->BSRRH = 1;
	 GPIOD->BSRRL = 2;

    for(i=0; i < 10; i++)
		 delai();

  }


}

